from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select

# 打开浏览器
driver = webdriver.Chrome()
sleep(2)
# 设置浏览器窗口大小
driver.set_window_size(800, 1000)
# 输出浏览器窗口大小
print(f'宽高：{driver.get_window_size()}')
sleep(2)
# 双击打开酒仙网页面
driver.get('https://www.jiuxian.com/')
sleep(2)
# 窗口最大化
driver.maximize_window()
# 输出浏览器窗口大小
print(f'宽高：{driver.get_window_size()}')
sleep(2)
# 输出浏览器信息
print(f"名称；{driver.name}, 标题：{driver.title}, URL：{driver.current_url}, 尺寸：{driver.get_window_size()}")
# 点击请登录
driver.find_element(By.CLASS_NAME, 'headerLogin').click()
sleep(2)
# 输入手机号
sjh = driver.find_element(By.ID, 'userName')
sjh.send_keys('15985645480')
# 输出输入框信息
print(f"文本值：{sjh.text}, class属性：{sjh.get_attribute('class')}, 宽高：{sjh.size}, 坐标：{sjh.location}, 元素是否可用：{sjh.is_enabled()}, 元素是否显示：{sjh.is_displayed()}, 元素是否被选中：{sjh.is_selected()}")
sleep(2)
# 输入密码
driver.find_element(By.ID, 'password').send_keys('gW09092434')
# 强制等待 用户点击图片输入验证码
sleep(10)
# 点击登录按钮
driver.find_element(By.NAME, 'bi_Usuallogin').click()
sleep(2)
# 点击确认
driver.find_element(By.ID, 'bindSub').click()
sleep(5)
# 搜索框：贵州茅台酒
seatch = driver.find_element(By.TAG_NAME, 'input')
seatch.send_keys('贵州茅台酒')
sleep(2)
# # 回车 return / enter
# seatch.send_keys(Keys.RETURN)
# sleep(2)
# 鼠标右击
ActionChains(driver).context_click(seatch).perform()
sleep(2)
# 鼠标三击
ActionChains(driver).double_click(seatch).click().perform()
# 键盘全选
# seatch.send_keys(Keys.CONTROL, 'a')
sleep(2)
# 键盘复制
seatch.send_keys(Keys.CONTROL, 'c')
sleep(2)
# 点击搜索按钮
driver.find_element(By.CSS_SELECTOR, '.search-btn').click()
sleep(2)
# 在结果中查找
cz = driver.find_element(By.XPATH, '//*[@id="extKey"]')
# 键盘粘贴
cz.send_keys(Keys.CONTROL, 'v')
sleep(2)
# 鼠标双击
ActionChains(driver).double_click(cz).perform()
sleep(2)
# 键盘剪切
cz.send_keys(Keys.CONTROL, 'x')
sleep(2)
# 键盘输入
cz.send_keys('省')
sleep(2)
# 键盘退格键
cz.send_keys(Keys.BACKSPACE)
sleep(2)
# 执行JavaScript设置光标
driver.execute_script("""
    var element = arguments[0];
    var position = arguments[1];
    element.focus();
    element.setSelectionRange(position, position);
""", cz, 1)
sleep(2)
# 键盘删除键
cz.send_keys(Keys.DELETE)
sleep(2)
# 键盘清空/清除
cz.clear()
sleep(2)
# 键盘复制
cz.send_keys(Keys.CONTROL, 'v')
sleep(2)
# 点击搜索图标
# driver.find_element(By.XPATH, '//a[@href="javascript:extSearch()"]').click()
# 回车enter
cz.send_keys(Keys.ENTER)
sleep(2)
# 鼠标悬停事件 : 我的酒仙
xt = driver.find_element(By.CSS_SELECTOR, 'body > div.topHeader > div > div.topHeaderRight > ul > li.hd-n1.dropTitle > div.myJiuxian > a')
ActionChains(driver).move_to_element(xt).perform()
sleep(2)
# 点击我的订单
driver.find_element(By.LINK_TEXT, '我的订单').click()
sleep(2)
# 窗口切换
driver.switch_to.window(driver.window_handles[-1])
sleep(2)
# 滚动
driver.execute_script('window.scrollTo(0, 300)')
sleep(2)
# 点击收货地址
driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[4]/a[3]/span').click()
sleep(2)
# 输入收件人
driver.find_element(By.NAME, 'consignee').send_keys('小高')
sleep(2)
# 选择地址
province = Select(driver.find_element(By.ID, 'province'))
province.select_by_value('2')
sleep(2)
city = Select(driver.find_element(By.ID, 'city'))
city.select_by_index(1)
sleep(2)
district = Select(driver.find_element(By.ID, 'district'))
district.select_by_visible_text('昌平区')
sleep(2)
# 输入详细地址
driver.find_element(By.NAME, 'addressMore').send_keys('南口南大街21号')
sleep(2)
# 输入邮政编号
driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/div[2]/div[2]/form/div/div[4]/input').send_keys('101010')
sleep(2)
# 输入手机号
driver.find_element(By.XPATH, '//*[@id="iphone"]').send_keys('15985645480')
sleep(2)
# 点击提交
driver.find_element(By.CSS_SELECTOR, '#save').click()
sleep(2)
# 添加成功  点击确定
driver.find_element(By.XPATH, '/html/body/div[8]/div/div[3]/span[1]/b').click()
sleep(2)
# 点击删除 地址
driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/div[2]/div[1]/table/tbody/tr[2]/td[4]/a[2]').click()
sleep(2)
# 确定删除  点击确定
driver.find_element(By.XPATH, '/html/body/div[8]/div/div[3]/span[1]/b').click()
sleep(2)
# 删除成功 点击确定
driver.find_element(By.XPATH, '/html/body/div[8]/div/div[3]/span[1]/b').click()
sleep(2)


sleep(10)
# driver.quit()









